package com.niiwoo.civet.user.service.local.portraitAuth;

import com.niiwoo.civet.user.enums.PortraitChannelEnum;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

@Component
public class PortraitAuthOperations implements ApplicationContextAware {

    private ApplicationContext ctx;

    private Map<PortraitChannelEnum, PortraitAuthOperation> portraitAuthOperationMap = new HashMap<>();

    @PostConstruct
    public void init() {
        Optional.ofNullable(ctx.getBeansOfType(PortraitAuthOperation.class))
                .ifPresent(m -> {
                    m.forEach((k, v) -> {
                        portraitAuthOperationMap.put(v.getSupportChannel(), v);
                    });
                });
    }

    public PortraitAuthOperation getByChannel(PortraitChannelEnum portraitChannel) {
        return portraitAuthOperationMap.get(portraitChannel);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext.getParent() == null) {
            this.ctx = applicationContext;
        }
    }


}
